
<template>
  <div>
    <!-- 轮播图部分 -->
    <div class="carousel-container">
      <!-- 轮播项 -->
      <div
          v-for="(slide, index) in slides"
          :key="index"
          class="carousel-slide"
          :class="{ active: currentIndex === index }"
      >
        <img :src="slide.image" class="slide-image" :alt="slide.title" />
        <div class="slide-content">
          <h2 class="slide-title">{{ slide.title }}</h2>
          <p class="slide-description">{{ slide.description }}</p>
        </div>
      </div>

      <!-- 导航控件 -->
      <div v-if="showNavigation">
        <div class="arrow-nav prev" @click="changeSlide(-1)">
          <div class="arrow prev"></div>
        </div>
        <div class="arrow-nav next" @click="changeSlide(1)">
          <div class="arrow next"></div>
        </div>

        <div class="carousel-controls" id="dots-container">
          <div
              v-for="(slide, index) in slides"
              :key="index"
              class="control-dot"
              :class="{ active: currentIndex === index }"
              @click="goToSlide(index)"
          ></div>
        </div>
      </div>
    </div>

    <!-- 新页面内容部分 -->
    <div class="page-section" v-for="(page, index) in pages" :key="index">
      <div class="page-content">
        <!-- 文字部分 -->
        <div class="page-text">
          <h2>{{ page.title }}</h2>
        </div>
        <!-- 图片部分 -->
        <img :src="page.image" alt="页面图片" class="page-image" />
      </div>
    </div>

  </div>
</template>

<script>
import image1 from '../assets/papercut.jpg'
import image2 from '../assets/creatpaper.jpg'
import image3 from '../assets/chin.jpg'
import pageImage1 from '../assets/page1.jpg'
import pageImage2 from '../assets/page2.jpg'
import pageImage3 from '../assets/page3.jpg'
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        {
          image: image1,
          title: '传统剪纸艺术',
          description: '中国剪纸是一种用剪刀或刻刀在纸上剪刻花纹的传统民间艺术，承载着中华民族深厚的历史文化内涵。'
        },
        {
          image: image2,
          title: '古法造纸技艺',
          description: '传承千年的手工造纸技艺，完整保留了东汉时期蔡伦发明的造纸工艺，是活化的古代科技标本。'
        },
        {
          image: image3,
          title: '龙泉青瓷烧制',
          description: '中国传统制瓷工艺的瑰宝，其釉色青翠如玉，造型端庄典雅，展现宋代极简美学巅峰。'
        }
      ],
      pages: [
        {
          title: '江苏苏绣--以针作绣，巧夺天工',
          image: pageImage2
        },
        {
          title: '山东鲁绣——丝线走千年，绣针绘乾坤 ',
          image: pageImage1
        },
        {
          title: '四川川剧--蜀戏冠天下，川腔蜀韵',
          image: pageImage3
        }
      ],
      autoPlay: null,
      showNavigation: true // 默认显示导航栏
    };
  },

  mounted() {
    this.startAutoPlay();
    window.addEventListener('scroll', this.handleScroll);
  },
  beforeUnmount() {
    this.stopAutoPlay();
    window.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
      const scrollY = window.scrollY; // 获取当前滚动位置
      const windowHeight = window.innerHeight; // 获取视口高度
      const currentSection = Math.floor(scrollY / windowHeight); // 计算当前部分索引

      console.log('Scroll Y:', scrollY);
      console.log('Current Section:', currentSection);
      console.log('Show Navigation:', currentSection < this.slides.length);

      // 如果滚动到新页面部分，隐藏导航栏
      this.showNavigation = currentSection < this.slides.length;

      if (currentSection < this.slides.length) {
        // 切换到对应的轮播项
        this.updateSlide(currentSection);
        this.resetAutoPlay();
      }
    },
    startAutoPlay() {
      this.autoPlay = setInterval(this.nextSlide, 5000);
    },
    stopAutoPlay() {
      clearInterval(this.autoPlay);
    },
    resetAutoPlay() {
      this.stopAutoPlay();
      this.startAutoPlay();
    },
    updateSlide(index) {
      this.currentIndex = index;
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length;
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
    },
    goToSlide(index) {
      this.updateSlide(index);
      this.resetAutoPlay();
      window.scrollTo({
        top: index * window.innerHeight,
        behavior: 'smooth' // 添加平滑滚动效果
      });
    },
    changeSlide(direction) {
      direction === 1 ? this.nextSlide() : this.prevSlide();
      this.resetAutoPlay();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.carousel-container {
  position: relative;
  width: 100%;
  height: 100vh; /* 轮播图高度与视口一致 */
  overflow: hidden;
  background: #f5f3ef;
}

.carousel-slide {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 1.5s ease-in-out, transform 1.5s ease-in-out;
  transform: scale(1.1);
}

.carousel-slide.active {
  opacity: 1;
  transform: scale(1);
}

.slide-image {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.8);
}

.slide-content {
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translateY(-50%);
  color: white;
  max-width: 600px;
  z-index: 2;
}

.slide-title {
  font-size: 3.5rem;
  font-weight: 300;
  margin-bottom: 1.5rem;
  opacity: 0;
  transform: translateX(-50px);
  transition: all 1s ease;
}

.slide-description {
  font-size: 1.2rem;
  line-height: 1.8;
  margin-bottom: 2rem;
  opacity: 0;
  transform: translateX(-50px);
  transition: all 1s ease 0.3s;
}

.carousel-slide.active .slide-title,
.carousel-slide.active .slide-description {
  opacity: 1;
  transform: translateX(0);
}

.carousel-controls {
  position: absolute;
  bottom: 5%;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 15px;
  z-index: 3;
  transition: opacity 0.5s ease; /* 添加过渡效果 */
}

.control-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: all 0.3s ease;
}

.control-dot.active {
  background: #fff;
  transform: scale(1.3);
}

.arrow-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  z-index: 3;
}

.arrow-nav:hover {
  background: rgba(0, 0, 0, 0.6);
}

.arrow-nav.prev {
  left: 2%;
}

.arrow-nav.next {
  right: 2%;
}

.arrow {
  width: 20px;
  height: 20px;
  border: solid white;
  border-width: 0 3px 3px 0;
}

.arrow.prev {
  transform: rotate(135deg);
  margin-left: 5px;
}

.arrow.next {
  transform: rotate(-45deg);
  margin-right: 5px;
}

.page-section {
  height: 100vh; /* 每个新页面高度与视口一致 */
  padding: 20px;
  background: #f5f3ef;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  transition: opacity 1s ease-in-out; /* 添加过渡效果 */
}
.page-section {
  position: relative;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.page-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 1;
}

.page-text {
  position: absolute;
  top: 20%;
  left: 10%;
  color: white;
  font-size: 2rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
  z-index: 2;
}
@media (max-width: 768px) {
  .page-text {
    top: 15%;
    left: 8%;
    font-size: 1.5rem;
  }
}

@media (max-width: 768px) {
  .slide-title {
    font-size: 2.5rem;
  }

  .slide-description {
    font-size: 1rem;
    max-width: 80%;
  }

  .arrow-nav {
    width: 40px;
    height: 40px;
  }

  .page-section {
    padding: 10px;
  }
}
</style>

